#include "../mq_server/consumer.h"
#include <gtest/gtest.h>

using namespace ns_consume;
shared_ptr<ConsumerManage> cmp;

class ConsumerTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cmp = std::make_shared<ConsumerManage>();
        cmp->init_queue_consumer("queue1");
    }
    virtual void TearDown() override
    {
        cmp->clear();
    }
};

void cb(const std::string &tag, const ns_proto::Attributes *bp, const std::string &body)
{
    std::cout << tag << " 消费了消息:" << body << std::endl;
}


TEST(consumer_test, insert_test)
{
    cmp->insert_queue_consumer("queue1", "consumer1", false, cb);
    cmp->insert_queue_consumer("queue1", "consumer2", false, cb);
    cmp->insert_queue_consumer("queue1", "consumer3", false, cb);

    ASSERT_EQ(cmp->exist("queue1", "consumer1"), true);
    ASSERT_EQ(cmp->exist("queue1", "consumer2"), true);
    ASSERT_EQ(cmp->exist("queue1", "consumer3"), true);
}

TEST(consumer_test, remove_test)
{
    cmp->delete_queue_consumer("queue1", "consumer1");

    ASSERT_EQ(cmp->exist("queue1", "consumer1"), false);
    ASSERT_EQ(cmp->exist("queue1", "consumer2"), true);
    ASSERT_EQ(cmp->exist("queue1", "consumer3"), true);
}

TEST(consumer_test, choose_test)
{
    Consumer::ptr cp = cmp->select_queue_consumer("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->_tag, "consumer2");

    cp = cmp->select_queue_consumer("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->_tag, "consumer3");

    cp = cmp->select_queue_consumer("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->_tag, "consumer2");
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);

    return RUN_ALL_TESTS();
}